﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Windows.Web.Syndication;
using System.Collections.ObjectModel;


namespace SoCalNews
    {
        // FeedData
        // Holds info for a single blog feed, including a list of blog posts (FeedItem)
        public class FeedData
        {
            public string Title { get; set; }
            public string Description { get; set; }
            public DateTime PubDate { get; set; }

            private List<FeedItem> _Items = new List<FeedItem>();
            public List<FeedItem> Items
            {
                get
                {
                    return this._Items;
                }
            }
            
        }

        // FeedItem
        // Holds info for a single blog post
        public class FeedItem
        {
            public string Title { get; set; }
            public string Category { get; set; }
            public string Author { get; set; }
            public string Content { get; set; }
            public DateTime PubDate { get; set; }
            public Uri Link { get; set; }
        }
        // FeedDataSource
        // Holds a collection of blog feeds (FeedData), and contains methods needed to
        // retreive the feeds.
        public class FeedDataSource
        {
            private ObservableCollection<FeedData> _Feeds = new ObservableCollection<FeedData>();
            public ObservableCollection<FeedData> Feeds
            {
                get
                {
                    return this._Feeds;
                }
            }
            public async Task GetFeedsAsync()
            {
                Task<FeedData> feed1 =
                    GetFeedAsync("http://www.ocregister.com/common/rss/rss.php");
                Task<FeedData> feed2 =
                    GetFeedAsync("http://feeds.latimes.com/latimes/news?format=rss");
                this.Feeds.Add(await feed1);
                this.Feeds.Add(await feed2);
            }

            private async Task<FeedData> GetFeedAsync(string feedUriString)
            {
                // using Windows.Web.Syndication;
                SyndicationClient client = new SyndicationClient();
                Uri feedUri = new Uri(feedUriString);

                try
                {
                    SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);
                    // This code is executed after RetrieveFeedAsync returns the SyndicationFeed.
                    // Process it and copy the data we want into our FeedData and FeedItem classes.
                    FeedData feedData = new FeedData();
                    feedData.Title = feed.Title.Text;
                    if (feed.Subtitle != null && feed.Subtitle.Text != null)
                    {
                        feedData.Description = feed.Subtitle.Text;
                    }
                    // Use the date of the latest post as the last updated date.
                    feedData.PubDate = feed.Items[0].PublishedDate.DateTime;
                    foreach (SyndicationItem item in feed.Items)
                    {
                        FeedItem feedItem = new FeedItem();
                        feedItem.Title = item.Title.Text;
                        if (item.Categories.Count > 0)
                        {
                            feedItem.Category = item.Categories[0].NodeValue;
                        }
                        feedItem.PubDate = item.PublishedDate.DateTime;
                        //feedItem.Author = item.Authors[0].Name.ToString();
                        // Handle the differences between RSS and Atom feeds.
                        feedItem.Content = item.Summary.Text.Replace("&quot;","\"").Replace("&#160;","");
                        feedItem.Link = item.Links[0].Uri;
                        feedData.Items.Add(feedItem);
                    }
                    return feedData;
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
    }
